package com.bw.demo.model;

public class BwResult<D>
{
	/**
	 * 0 为失败，1为成功
	 */
	public static final String SUCCESS = "0";
	public static final String FAIL = "1";

	private String code;
	private D data;
	private String msg;

	private BwResult(String code)
	{
		this.code = code;
	}

	public static <T> BwResult<T> get(String code)
	{
		return new BwResult<>(code);
	}

	public static <T> BwResult<T> success()
	{
		return new BwResult<T>(SUCCESS).setMsg("操作成功");
	}

	public static <T> BwResult<T> success(T data)
	{
		return new BwResult<T>(SUCCESS).setMsg("操作成功").setData(data);
	}

	public static <T> BwResult<T> success(String msg)
	{
		return new BwResult<T>(SUCCESS).setMsg(msg);
	}

	public static <T> BwResult<T> success(String msg, T data)
	{
		return new BwResult<T>(SUCCESS).setMsg(msg).setData(data);
	}

	public static <T> BwResult<T> fail()
	{
		return new BwResult<T>(FAIL).setMsg("操作失败");
	}

	public static <T> BwResult<T> fail(String msg)
	{
		return new BwResult<T>(FAIL).setMsg(msg);
	}

	public static <T> BwResult<T> fail(String msg, T data)
	{
		return new BwResult<T>(FAIL).setMsg(msg).setData(data);
	}

	public static <T> BwResult<T> fail(T data)
	{
		return new BwResult<T>(FAIL).setMsg("操作失败").setData(data);
	}

	public D getData()
	{
		return this.data;
	}

	public BwResult<D> setData(D data)
	{
		this.data = data;
		return this;
	}

	public String getCode()
	{
		return code;
	}

	public BwResult<D> setCode(String code)
	{
		this.code = code;
		return this;
	}

	public String getMsg()
	{
		return msg;
	}

	public BwResult<D> setMsg(String msg)
	{
		this.msg = msg;
		return this;
	}

	/**
	 * 重载空参for 跨服务调用实例化该类用的构造函数
	 */
	private BwResult()
	{
	}
}